#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MaxN 1001
#define MinH -10001
#define ERROR 0

typedef enum { false, true } bool;
typedef int ElementType;
typedef struct HNode* Heap; 
struct HNode {
    ElementType* Data; 
    int Size;         
    int Capacity;     
}H;

struct Huffman_Codes {
    char code[200];
    int Code_Len;
};
typedef struct Huffman_Codes Hfm_Code;
typedef struct Huffman_Codes* Hfm_Code_p;

typedef struct TNode* Position;
typedef Position BinTree; 
struct TNode {
    ElementType Flag;
    BinTree Left;   
    BinTree Right;  
};

void Create(int N)
{
    H.Size = 0;
    H.Data = (ElementType*)malloc((N + 1) * sizeof(ElementType));
    H.Data[0] = MinH;
    H.Capacity = N;
}

bool IsFull(Heap H)
{
    return (H->Size == H->Capacity);
}

void Insert(ElementType X)
{
    if (IsFull(&H))return;

    int i;
    for (i = ++H.Size; H.Data[i / 2] > X; i /= 2)
        H.Data[i] = H.Data[i / 2];

    H.Data[i] = X;
}

ElementType Delete(void)
{
    if (H.Size == 0)return ERROR;

    ElementType temp, min;
    min = H.Data[1];
    temp = H.Data[H.Size--];
    int Parent, Child;
    for (Parent = 1; Parent * 2 <= H.Size; Parent = Child)
    {
        Child = Parent * 2;
        if ((Child != H.Size) && (H.Data[Child] > H.Data[Child + 1]))
        {
            Child++;
        }
        if (temp <= H.Data[Child])break;
        else H.Data[Parent] = H.Data[Child];
    }
    H.Data[Parent] = temp;
    return min;
}

int Get_WPL(int N, int* c)
{
    int count = 0;
    Create(N);
    for (count; count < N; count++)
    {
        char ch;
        int temp;
        scanf(" %c", &ch);
        scanf("%d", &temp);
        c[count] = temp;
        Insert(temp);
    }
    int WPL = 0;
    while (H.Size >= 2)
    {
        int a = 0, b = 0, sum = 0;
        a = Delete();
        b = Delete();
        sum = a + b;
        WPL += sum;
        Insert(sum);
    }
    return WPL;
}

bool Get_Check_Len(int WPL, Hfm_Code_p Code, int* c, int N)
{
    bool flag = true;

    //Code = (Hfm_Code_p)malloc(N * sizeof(Hfm_Code));
    int sum = 0;
    for (int i = 0; i < N; i++)
    {
        char ch;
        scanf("%c ", &ch);
        gets(Code[i].code);
        if (flag)
        {
            Code[i].Code_Len = strlen(Code[i].code);
            if (Code[i].Code_Len > N - 1) flag = false;
            sum += (Code[i].Code_Len * c[i]);
                  
        }
    }
    if (sum == WPL && flag)flag = true;
    else flag = false;
    return flag;
}


bool Check_Tree(Hfm_Code_p Code, int N)
{
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (i == j)continue;
            if (strncmp(Code[i].code, Code[j].code, Code[i].Code_Len) == 0)return false;
        }
    }
    return true;
}

int main(void)
{
    int N;
    scanf("%d", &N);
    int* c = (int*)malloc(N * sizeof(int));
    int WPL = Get_WPL(N, c);
    
    int M;
    scanf("%d", &M);
    getchar();


    while (M--)
    {
        Hfm_Code_p Code;
        Code = (Hfm_Code_p)malloc(N * sizeof(Hfm_Code));
        bool flag_L = Get_Check_Len(WPL, Code, c, N);
        if (!flag_L)
        {
            printf("No\n");
            continue;
        }

        bool flag_T = Check_Tree(Code, N);
        if (flag_L && flag_T)
            printf("Yes\n");
        else
            printf("No\n");
             
    }
    return 0;
}



